const path = require('path');
const fs = require('fs');
const { execSync } = require('child_process');
const chalk = require('chalk');

function dos2unix(dirPaths, extName = 'sh') {
  if (typeof dirPaths !== 'string' && !Array.isArray(dirPaths)) {
    console.log(chalk.red(`${dirPaths} 必须是或者字符串`));
    return;
  }
  if (typeof dirPaths === 'string') {
    dirPaths = [dirPaths];
  }

  dirPaths.forEach(dirPath => {
    dirPath = path.resolve(__dirname, dirPath);
    try {
      const stat = fs.statSync(dirPath);
      if (!stat.isDirectory()) {
        return;
      }
      const files = fs.readdirSync(dirPath);
      const targetFiles = files.filter(file => file.endsWith(extName));
      if (targetFiles.length > 0) {
        targetFiles.forEach(targetFile => {
          execSync(`dos2unix ${targetFile}`);
        });
      }
    } catch (err) {
      console.log(chalk.red('error', dirPath));
    }
  });
}

dos2unix('../libs/nvm')

